Repository navigation
Conversation
| } | ||
| dictkeys_incref(keys); | ||
| clear_common(old); | ||
| for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) { |
There was a problem hiding this comment.
dk_nentries can be modified by another thread. How about using v->capacity instead?
There was a problem hiding this comment.
good catch, switched to v->capacity.
| class A: ... | ||
| a = A() | ||
| a.x = 1 | ||
| f = as_frozendict(a.__dict__) |
There was a problem hiding this comment.
I would prefer to check also a just after as_frozendict() call:
| f = as_frozendict(a.__dict__) | |
| f = as_frozendict(a.__dict__) | |
| self.assertEqual(vars(a), {}) |
| ASSERT_CONSISTENT(old); | ||
| } |
There was a problem hiding this comment.
IMO it's also worth it to check dict consistency for the if (values && values->embedded) case, so:
| ASSERT_CONSISTENT(old); | |
| } | |
| } | |
| ASSERT_CONSISTENT(old); |
| } | ||
|
|
||
| // Transfer keys and values from dict to frozendict: | ||
| new->ma_used = used; |
There was a problem hiding this comment.
| new->ma_used = used; | |
| // Only transfer keys and values to the new dict | |
| // once the old dict is cleared | |
| new->ma_used = used; |
|
done, applied all three. |
|
cc @sobolevn |
sobolevn
left a comment
There was a problem hiding this comment.
Maybe we should disallow values with ->embedded set?
The whole point of this API is to use mutable dict objects to create new frozendict objects efficently.
Using embedded dicts do not really make sense in my opinion.
I would just raise SystemError for this case.
| a = A() | ||
| a.x = 1 | ||
| f = as_frozendict(a.__dict__) | ||
| self.assertEqual(vars(a), {}) |
There was a problem hiding this comment.
It feels weird that this mutates instance's __dict__ :(
| } | ||
| dictkeys_incref(keys); | ||
| clear_common(old); | ||
| for (Py_ssize_t i = 0; i < v->capacity; i++) { |
There was a problem hiding this comment.
Maybe invalidate_and_clear_inline_values(v); instead?
| PyErr_NoMemory(); | ||
| return -1; | ||
| } | ||
| dictkeys_incref(keys); |
There was a problem hiding this comment.
Question: why do we need the incref here?
Copy the inline values into the new frozendict and clear them on the object, instead of leaving them shared.